package com.adgox.tiantianbiting.mine.tradeRecord;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.adapter.TradeRecordAdapter;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.TradeRecordBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TradeRecordActivity extends AppCompatActivity {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    private int userId;
    private TradeRecordAdapter adapter;
    private int currentPage = 1;
    private int maxPage = 20;
    private int totalPages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade_record);
        ButterKnife.bind(this);
        init();
    }

    private void init() {

        UserInfo userInfo = SessionManager.getDefault().getUser();
        userId = userInfo.getId();

        //这里应该只用请求这一个接口了吧，就不用mvp了，直接写在这里了
        getData();
    }

    private void getData() {
        OkGo.<BaseResponseBean<PageBean<TradeRecordBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_RECORD + userId + "/0")
                .params("startPoint", currentPage)
                .params("maxPage", maxPage)
                .execute(new JsonCallback<BaseResponseBean<PageBean<TradeRecordBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<PageBean<TradeRecordBean>>> response) {
                        onSetData(response.body().getData());
                    }
                });
    }

    private void onSetData(PageBean<TradeRecordBean> data) {
        if (data == null||data.getData()==null||data.getData().size()==0) {
            llEmpty.setVisibility(View.VISIBLE);
            llContent.setVisibility(View.GONE);
            return;
        }

        totalPages = data.getTotalPages();

        if (adapter == null) {
            LinearLayoutManager manager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(manager);
            adapter = new TradeRecordAdapter(data.getData());
            recyclerView.setAdapter(adapter);
        } else {
            adapter.addDatas(data.getData());
            adapter.notifyDataSetChanged();
        }
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
                int lastVisibleItemPosition = lm.findLastVisibleItemPosition();
                int childCount = lm.getChildCount();
                int itemCount = lm.getItemCount();
                int scrollState = recyclerView.getScrollState();

                if (childCount > 0 && lastVisibleItemPosition == itemCount - 1 &&scrollState==RecyclerView.SCROLL_STATE_IDLE) {
                    if (currentPage < totalPages) {
                        currentPage++;
                        getData();
                    } else {
                        Toast.makeText(TradeRecordActivity.this, "没有更多了", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }
}
